import pandas as pd
import matplotlib.pyplot as plt

# 加载数据
df = pd.read_csv('Week_ Four/titanic.csv')

# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei']

# 设置画布大小
plt.figure(figsize=(12, 10))

# （1）统计各仓乘客比例额
total_passengers = df['Pclass'].value_counts()
plt.subplot(2, 2, 1)
plt.pie(total_passengers, labels=total_passengers.index, autopct='%1.1f%%')
plt.title('各仓乘客比例额')

# （2）统计各仓幸存者比例
survivors = df[df['Survived'] == 1]
survivors_by_class = survivors['Pclass'].value_counts()
plt.subplot(2, 2, 2)
plt.pie(survivors_by_class, labels=survivors_by_class.index, autopct='%1.1f%%')
plt.title('xincunzhe')

# （3）统计各仓遇难者比例
non_survivors = df[df['Survived'] == 0]
non_survivors_by_class = non_survivors['Pclass'].value_counts()
plt.subplot(2, 2, 3)
plt.pie(non_survivors_by_class, labels=non_survivors_by_class.index, autopct='%1.1f%%')
plt.title('各仓遇难者比例')

# （4）统计遇难者年龄划分情况
age_bins = [0, 18, 45, 65, float('inf')]
age_labels = ['0 - 18', '19 - 45', '46 - 65', '65+']
non_survivors['AgeGroup'] = pd.cut(non_survivors['Age'], bins=age_bins, labels=age_labels)
age_group_counts = non_survivors['AgeGroup'].value_counts()
plt.subplot(2, 2, 4)
plt.bar(age_group_counts.index, age_group_counts)
plt.title('遇难者年龄划分情况')

plt.tight_layout()
plt.show()